package com.poc.party.controller;

import com.meta.common.CommonInfo;
import com.meta.nd.annotation.utils.NdAnnotationUtil;
import com.meta.nd.vo.*;
import com.poc.party.bo.PartyNv;
import com.poc.party.vo.annotation.PartyBO;
import com.meta.util.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.meta.common.ConstDef;
import com.meta.nd.service.NvService;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class PartyController {

	
	@Autowired
	NvService nvService;
	

	@RequestMapping(value = "/party/savePartyNv", method = { RequestMethod.POST })
	public ReturnMessage<PartyNv> savePartyNv(@RequestBody RequestMessage<PartyNv> requestMessage) {
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		PartyNv partyNv =  requestMessage.getValue();
		try {
			long id = nvService.saveNv(commonInfo,partyNv);
			return new ReturnMessage<PartyNv>(partyNv,true);
		}catch (Exception e ){
			log.error(e.getMessage(),e);
			return ExceptionUtils.buildReturnMessage(e);
		}
	}

	@RequestMapping("/party/getPartyNv") // insert for NdNd
	public ReturnMessage<PartyNv> getPartyNv( @RequestBody RequestMessage<Long> requestMessage) {
		try {
			CommonInfo commonInfo = requestMessage.getCommonInfo();
			long id = requestMessage.getValue();
			PartyNv partyNv = (PartyNv) nvService.getNv(commonInfo,ConstDef.ND_CODE_PARTY_ND,id);
			return new ReturnMessage<PartyNv>(partyNv, true);
		}catch (Exception e ){
			log.error(e.getMessage(),e);
			return ExceptionUtils.buildReturnMessage(e);
		}
	}
	@RequestMapping(value = "/party/savePartyNvs", method = { RequestMethod.POST }) // insert for NdNd
	public ReturnMessage<List<PartyNv>> savePartyNvs(@RequestBody RequestMessage<List<PartyNv>> requestMessage) {
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		List<PartyNv> partyNvs =  requestMessage.getValue();
		List<Nv> nvs = new ArrayList<>();
		nvs.addAll(partyNvs);
		try {
			 nvService.saveNvs(commonInfo,nvs);
			partyNvs.clear();
			for( Nv nv : nvs )
			partyNvs.add((PartyNv) nv);
			return new ReturnMessage<List<PartyNv>>(partyNvs,true);
		}catch (Exception e ){
			log.error(e.getMessage(),e);
			return ExceptionUtils.buildReturnMessage(e);
		}
	}


@RequestMapping("/party/getPartyBO") // insert for NdNd
public ReturnMessage<PartyBO> getPartyBO(@RequestBody RequestMessage<Long> requestMessage) {
	try {
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		long id = requestMessage.getValue();
		Nv partyNv = nvService.getNv(commonInfo,ConstDef.ND_CODE_PARTY_ND,id);
		PartyBO partyBO = NdAnnotationUtil.nv2Object(partyNv,PartyBO.class);
		//NdAnnotationUtil.object2Nv(partyBO);
		return new ReturnMessage<PartyBO>(partyBO, true);
	}catch (Exception e ){
		log.error(e.getMessage(),e);
		return ExceptionUtils.buildReturnMessage(e);
	}
}



}
